<?php  
namespace app\index\controller;
use \think\Request;
use app\extend\lib\admin;
use \think\Session;
use \think\Db;
class Database
{
    public function find(){
        $db = db('user')->where('id',1)->find();
        print_r($db);
    }
    public function userTable(){
        $db = db('user')->column('Id,username,level');
        $cou = db('user')->count();
        $res = array(
            'code'=>0
            ,'msg'=>'success'
            ,'count'=>$cou
            ,'data'=>$db
        );
        return json($res);
    }
	public function userLogin() 
	{
		$username = input('username');
		$password = input('password');
		if(Session::has('user')&&(Session::get('user')->name)!=null){
			$admin = Session::get('user');
			$user = array('username'=> $admin->name,'level' => $admin->level ,'code' => 4);
		}else{
			$db = Db::table('user')->where('username',$username)->find();
			// print_r($db);
			echo Session::has('user');
			if(!empty($db)){
				if($password == $db['password']){
					$user = array('username' => $db['username'], 'level' => $db['level'],'code' => 0);
					$user1 = new user();
					$user1->setName($db['username']);
					$user1->setLevel($db['level']);

					Session::set('user',$user1);
				}else{
					$user = array('code' => 1);
				}				
			}else{
				$user = array('code' => 2);
			}
		}
		return json($user);
	}
}
?>